package com.ruoyi.yscggl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.yscggl.domain.bo.ShbBo;
import com.ruoyi.yscggl.domain.vo.ShbVo;
import com.ruoyi.yscggl.domain.Shb;
import com.ruoyi.yscggl.mapper.ShbMapper;
import com.ruoyi.yscggl.service.IShbService;

import java.util.List;
import java.util.Collection;

/**
 * 审核进度Service业务层处理
 *
 * @author jiayuan
 * @date 2023-04-24
 */
@RequiredArgsConstructor
@Service
public class ShbServiceImpl implements IShbService {

    private final ShbMapper baseMapper;

    /**
     * 查询审核进度
     */
    @Override
    public ShbVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询审核进度列表
     */
    @Override
    public TableDataInfo<ShbVo> queryPageList(ShbBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Shb> lqw = buildQueryWrapper(bo);
        Page<ShbVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询审核进度列表
     */
    @Override
    public List<ShbVo> queryList(ShbBo bo) {
        LambdaQueryWrapper<Shb> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Shb> buildQueryWrapper(ShbBo bo) {
        LambdaQueryWrapper<Shb> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPnNo()), Shb::getPnNo, bo.getPnNo());
        lqw.eq(StringUtils.isNotBlank(bo.getYwlx()), Shb::getYwlx, bo.getYwlx());
        lqw.eq(StringUtils.isNotBlank(bo.getShrbh()), Shb::getShrbh, bo.getShrbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShr()), Shb::getShr, bo.getShr());
        lqw.eq(bo.getShrq() != null, Shb::getShrq, bo.getShrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShyj()), Shb::getShyj, bo.getShyj());
        lqw.eq(StringUtils.isNotBlank(bo.getShtz()), Shb::getShtz, bo.getShtz());
        return lqw;
    }

    /**
     * 新增审核进度
     */
    @Override
    public Boolean insertByBo(ShbBo bo) {
        Shb add = BeanUtil.toBean(bo, Shb.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改审核进度
     */
    @Override
    public Boolean updateByBo(ShbBo bo) {
        Shb update = BeanUtil.toBean(bo, Shb.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Shb entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除审核进度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
